<template>
  <view>个人中心设置页面</view>
  <!-- <view class="pd">
    <uni-list>
      <view>
        <uni-list-item
          v-for="(item, index) in uniList"
          :key="index"
          v-if="item.isShow"
          showArrow
          :title="item.title"
          @click="open(item.type)"
          clickable
        >
          <template v-slot:footer v-if="index == 2">
            {{ currentSize | format }}
          </template>
        </uni-list-item>
      </view>
    </uni-list>
    <button
      class="mainBg"
      style="border-radius: 200rpx; color: #fff"
      v-if="isLogin"
      @click="outLogin"
    >
      退出登录
    </button>
  </view> -->
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      currentSize: 0, //内存大小
      uniList: [
        {
          title: "账号与安全",
          type: "account",
          isShow: this.isLogin,
        },
        {
          title: "资料编辑",
          type: "infoEdit",
          isShow: this.isLogin,
        },
        {
          title: "清除缓存",
          type: "none",
          isShow: true,
        },
        {
          title: "意见反馈",
          type: "feedback",
          isShow: true,
        },
        {
          title: "关于社区",
          type: "about",
          isShow: true,
        },
      ],
    };
  },
  onLoad() {
    this.uniList = [
      {
        title: "账号与安全",
        type: "account",
        isShow: this.isLogin,
      },
      {
        title: "资料编辑",
        type: "infoEdit",
        isShow: this.isLogin,
      },
      {
        title: "清除缓存",
        type: "none",
        isShow: true,
      },
      {
        title: "意见反馈",
        type: "feedback",
        isShow: true,
      },
      {
        title: "关于社区",
        type: "about",
        isShow: true,
      },
    ];
    this.getStorageInfo();
  },
  computed: {
    ...mapState(["isLogin", "userInfo"]),
  },
  filters: {
    format(value) {
      return value > 1024
        ? (value / 1024).toFixed(2) + "MB"
        : value.toFixed(2) + "KB";
    },
  },

  methods: {
    open(type) {
      if (type === "none") {
        this.clear();
      }
      console.log(type);
      uni.navigateTo({
        url: `../user-${type}/user-${type}`,
        // url:"../user-account/user-account"
      });
    },
    // 获取缓存
    getStorageInfo() {
      let res = uni.getStorageInfoSync();
      console.log(res.currentSize);
      this.currentSize = res.currentSize;
    },
    // 清除缓存
    clear() {
      uni.showModal({
        title: "提示",
        content: "是否要清除所有缓存？",
        cancelText: "不清除",
        confirmText: "清除",
        success: (res) => {
          if (res.confirm) {
            uni.clearStorageSync();
            this.getStorageInfo();
            uni.showToast({
              title: "清除成功",
              icon: "none",
            });
          }
        },
      });
    },
    outLogin() {
      uni.removeStorageSync("isLogin");
      uni.removeStorageSync("userInfo");
      uni.removeStorageSync("token");
      this.$store.commit("outLogin");
      uni.navigateBack({});
    },
  },
};
</script>

<style></style>
